package com.facebank.usersupport.controller;

import com.facebank.usersupport.model.Associator;
import com.facebank.usersupport.model.RestModel;
import com.facebank.usersupport.service.IAssociatorService;
import com.facebank.usersupport.util.ImgSaveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class AssociatorController {

    @Autowired
    private IAssociatorService associatorService;

    /**
     * 根据需求返回一个客户列表
     * @param pageNo
     * @param length
     * @param associatorName
     * @param phone
     * @param status
     * @param starttime
     * @param endtime
     * @return
     */
    @RequestMapping("/associator/list")
    public RestModel list(@RequestParam(required = false, defaultValue = "1") int pageNo,
                          @RequestParam(required = false, defaultValue = "6") int length,
                          String associatorName, String phone, Short status , String starttime, String endtime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = null;
        long startTime = 0, endTime = 0;
        if (starttime != "" && starttime != null) {
            try {
                time1 = sdf.parse(starttime);
                startTime = time1.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Date time2 = null;
        if (endtime != "" && endtime != null) {
            try {
                time2 = sdf.parse(endtime);
                endTime = time2.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return associatorService.list(pageNo, length, associatorName, phone, status, startTime, endTime);
    }

    /**
     * 通过手机号新增一个客户
     * @param phone
     * @return
     */
    @RequestMapping("/associator/add")
    public RestModel add(String phone){
        return associatorService.add(phone);
    }
    /**
     * 增一个客户
     * @param associator
     * @return
     */
    @RequestMapping("/associator/addByDetail")
    public RestModel addByDetail(Associator associator,@RequestParam(value = "file", required = false) MultipartFile file){
        String path = "static/images/upload/";
        if (file != null) {
            String targetImgName = ImgSaveUtil.uploadImg(file, path);
            associator.setHeadImg(targetImgName);
        }
        return associatorService.addByDetail(associator);
    }
    /**
     * 通过id删除一个客户
     * @param associatorId
     * @return
     */
    @RequestMapping("/associator/delete")
    public RestModel delete(Long associatorId){
        return associatorService.delete(associatorId);
    }

    /**
     * 通过associator对象修改客户
     * @param associator
     * @return
     */
    @RequestMapping("/associator/update")
    public RestModel update(Associator associator,@RequestParam(value = "file", required = false) MultipartFile file){
        String path = "static/images/upload/";
        if (file != null) {
            String targetImgName = ImgSaveUtil.uploadImg(file, path);
            associator.setHeadImg(targetImgName);
        }
        return associatorService.update(associator);
    }




}
